package com.letv.android.client.album.controller;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.letv.android.client.activity.AlbumPlayActivity;
import com.letv.android.young.client.R;
import com.letv.business.flow.album.AlbumPlayFlowObservable.PlayErrorCodeNotify;

import java.util.Observable;
import java.util.Observer;

/**
 * 点播出现错误覆盖videoview时，显示的返回、标题、全半屏切换
 *
 * @author zhuqiao
 *
 */
public class AlbumErrorTopController implements OnClickListener, Observer {
    private AlbumPlayActivity mActivity;

    private View mContainView;
    private View mBackView;
    private TextView mTitleTextView;
//    private ImageView mSwitchView;
    /**
     * 错误码
     */
    private TextView mErrorCodeText;

    public AlbumErrorTopController(AlbumPlayActivity activity) {
        mActivity = activity;
        init();
    }

    private void init() {
        mContainView = mActivity.findViewById(R.id.album_error_top_frame);
        mBackView = mActivity.findViewById(R.id.album_error_top_back);
        mTitleTextView = (TextView) mActivity.findViewById(R.id.album_error_top_title);
//        mSwitchView = (ImageView) mActivity.findViewById(R.id.album_error_top_fullhalf_icon);
        mErrorCodeText = (TextView) mActivity.findViewById(R.id.player_error_code_text);

        mBackView.setOnClickListener(this);
//        mSwitchView.setOnClickListener(this);
    }

    public void show() {
        mContainView.setVisibility(View.VISIBLE);
    }

    public void hide() {
        mContainView.setVisibility(View.GONE);
    }

    public void setTitle(String title) {
        mTitleTextView.setText(title);
    }

    public void setVisibilityForSwitchView(int visibility) {
//        mSwitchView.setVisibility(visibility);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.album_error_top_back) {
            clickBack();
        }
//        else if (id == R.id.album_error_top_fullhalf_icon) {
//            switchScreen();
//        }

    }

    /**
     * 返回
     */
    private void clickBack() {
        if (mActivity.getController() == null) {
            return;
        }

        mActivity.getController().back();
//        if (!UIsUtils.isLandscape(mActivity)) {
//            mActivity.getController().back();
//        } else {
//            if (!mActivity.getController().fullBack()) {
//                mActivity.getController().half();
//            }
//        }
    }

    /**
     * 切换横竖屏
     */
    private void switchScreen() {
        if (mActivity.getController() == null || mActivity.getFlow() == null) {
            return;
        }

        mActivity.getController().full();
//        if (UIsUtils.isLandscape(mActivity)) {
//            mActivity.getController().half();
//        } else {
//            mActivity.getController().full();
//        }
    }

    /**
     * 设置错误码
     *
     * @param errorCode
     * @param toShow
     */
    public void setPlayErrorCode(String errorCode, boolean toShow) {
        if (toShow) {
            mErrorCodeText.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(errorCode)) {
                mErrorCodeText.setText("");
            } else {
                mErrorCodeText.setText(mActivity.getResources().getString(R.string.play_error_code, errorCode));
            }
        } else {
            mErrorCodeText.setVisibility(View.GONE);
        }
    }

    /**
     * 错误码是否可见
     *
     * @return
     */
    public boolean isErrorCodeVisible() {
        return mErrorCodeText.getVisibility() == View.VISIBLE;
    }

    public boolean isVisible() {
        return mContainView.getVisibility() == View.VISIBLE;
    }

    @Override
    public void update(Observable observable, Object data) {
        if (data instanceof String) {
//            String notify = (String) data;
//            if (TextUtils.equals(ScreenObservable.ON_CONFIG_CHANGE, notify)) {
//                if (UIsUtils.isLandscape(mActivity)) {
//                    mSwitchView.setImageResource(R.drawable.letv_play_ablum_half_selecter);
//                } else {
//                    mSwitchView.setImageResource(R.drawable.letv_full_selecter);
//                }
//            }
        } else if (data instanceof PlayErrorCodeNotify) {
            // 设置错误码
            PlayErrorCodeNotify notify = (PlayErrorCodeNotify) data;
            setPlayErrorCode(notify.errorCode, notify.shouldShow);
        }
    }
}
